import axios from "axios";
import {
  Message,
  Modal
} from "view-design";
import codeMsg from "./codeMsg";
import util from "./util";
let Axiosinstance = null;
// axios.defaults.withCredentials = true;
class httpRequest {
  constructor() {
    this.options = {
      method: "",
      url: "",
    };
    // 存储请求队列
    this.queue = {};
  }
  // 销毁请求实例
  destroy(url) {
    delete this.queue[url];
    const queue = Object.keys(this.queue);
    return queue.length;
  }
  // 请求拦截
  interceptors(instance, url) {
    // 添加请求拦截器
    instance.interceptors.request.use(
      (config) => {
        if (config.url != "/login") {
          let Token = sessionStorage.getItem("Token");
          config.headers["Authorization"] = Token;
        }
        console.log("请求参数", config);

        return config;
      },
      (error) => {
        // 对请求错误做些什么
        return Promise.reject(error);
      }
    );

    // 添加响应拦截器
    instance.interceptors.response.use(
      (res) => {
        console.log("响应数据", res);

        let {
          data,
          config
        } = res;
        if (data.Code != 0 && data.Code != undefined) {
          if (codeMsg[data.Code]) {
            if (data.Code == 6) {
              Modal.error({
                title: "通知",
                content: codeMsg[data.Code],
                onOk: () => {
                  window.location.href = "/#/";
                },
              });
            } else {
              Message.error(data.Message || codeMsg[data.Code]);
            }
          } else {
            Message.error("操作失败,请重新刷新页面");
          }
        } else {
          if (config.url.includes("/login")) {
            sessionStorage.setItem("Token", data.Data.Token);
            sessionStorage.setItem(
              "wsUrl",
              res.request.responseURL.replace("/login", "")
            );
          }
        }
        return data;
      },
      (error) => {
        if (error.toString().includes("404")) {
          Message.error("无效的请求");
        }
        if (error.toString().includes("401")) {
          Message.error("身份凭证过期，请重新登录");
          window.location.href = "/#/";
        }
        if (error.toString().includes("500")) {
          Message.error("服务器内部错误");
        }

        return Promise.reject(error);
      }
    );
  }
  // 创建实例
  create(options) {
    let conf = {
      baseURL: options.baseURL || util.baseUrl,
      headers: {
        "Content-Type": "application/json; charset=utf-8",
      },

    };
    if (options.responseType) {
      conf.responseType = options.responseType;
    }
    return axios.create(conf);
  }
  // 请求实例
  request(options) {
    Axiosinstance = this.create({});
    if (options.url == "/storage/get_file") {
      Axiosinstance = this.create({
        responseType: "blob"
      })
    }

    this.interceptors(Axiosinstance, options.url);
    options = Object.assign({}, options);
    this.queue[options.url] = Axiosinstance;
    return Axiosinstance(options);
  }
}
export default new httpRequest();